<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.stms.action.Stms9006ActionBean"%>
<%@page import="com.feib.stms.pagination.PaginatedList"%>
<%
// 欄位排序處理
Stms9006ActionBean actionBean = (Stms9006ActionBean) request.getAttribute("actionBean");
if (null != actionBean && null != actionBean.getQueryResult())
{
	PaginatedList queryResult = actionBean.getQueryResult();
	int sortDirection = queryResult.getSortDirection();  //2 : ascending, else: descending
	String sortSpan = "<span class=\"ui-icon ui-icon-circle-triangle-n\"></span>"; //由小到大
	if (1 == sortDirection)
	{
		sortSpan = "<span class=\"ui-icon ui-icon-circle-triangle-s\"></span>"; //由大到小
	}
	
	String sortCriterion = queryResult.getSortCriterion();
	if (null != sortCriterion && 0 != sortCriterion.length())
	{
		if ("userId".equalsIgnoreCase(sortCriterion))
		{
			pageContext.setAttribute("userIdSortedClass", 1==sortDirection?"sortDesc":"sortAsc");
			pageContext.setAttribute("userIdSortedSpan", sortSpan);
		}
		else if ("userName".equalsIgnoreCase(sortCriterion))
		{
			pageContext.setAttribute("userNameSortedClass", 1==sortDirection?"sortDesc":"sortAsc");
			pageContext.setAttribute("userNameSortedSpan", sortSpan);
		}
		else if ("userPhoneNo".equalsIgnoreCase(sortCriterion))
		{
			pageContext.setAttribute("userPhoneNoSortedClass", 1==sortDirection?"sortDesc":"sortAsc");
			pageContext.setAttribute("userPhoneNoSortedSpan", sortSpan);
		}
		else if ("userEmail".equalsIgnoreCase(sortCriterion))
		{
			pageContext.setAttribute("userEmailSortedClass", 1==sortDirection?"sortDesc":"sortAsc");
			pageContext.setAttribute("userEmailSortedSpan", sortSpan);
		}
		else if ("userLoginControl".equalsIgnoreCase(sortCriterion))
		{
			pageContext.setAttribute("userLoginControlSortedClass", 1==sortDirection?"sortDesc":"sortAsc");
			pageContext.setAttribute("userLoginControlSortedSpan", sortSpan);
		}
	}
	
	// 上下頁鈕處理
	int lastPageNumber = queryResult.getTotalPageCount();
	int currentPageNumber = queryResult.getPageNumber();
	int prePageNumber = currentPageNumber - 1;
	int nextPageNumber = currentPageNumber + 1;
	
	pageContext.setAttribute("lastPageNumber", new Integer(lastPageNumber));
	pageContext.setAttribute("currentPageNumber", new Integer(currentPageNumber));
	pageContext.setAttribute("prePageNumber", new Integer(prePageNumber));
	pageContext.setAttribute("nextPageNumber", new Integer(nextPageNumber));
} // end of if (null != actionBean && null != actionBean.getQueryResult())

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
            
    <script type="text/javascript">
    <!-- 
    $(document).ready(function() {
    
    	$('#doQueryBtn').click(function() {
    		return true;
		});
		
<c:if test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		
		$("[id^='viewDetail-']").click(function() {
			var pos = this.id.indexOf("viewGroup");
			var pos2 = this.id.lastIndexOf("-");
			var qryUserId = this.id.substring(11 ,pos - 1);
			var qryGroupPk = this.id.substring(pos2 + 1);

			
			$('#hidden\\.qryUserId').val(qryUserId);
			$('#hidden\\.qryGroupPk').val(qryGroupPk);
			
			$("#hidden\\.doUpdateBtn").trigger('click');
		});
		
		$("[id^='a-sortHeader-']").click(function() {
			var sortField = this.id.substring(13);
			$('#hidden\\.queryResult\\.sortCriterion').val(sortField);
			
			var clazz = $(this).attr('class');
			if (null == clazz || 0 == clazz.length)
				$('#hidden\\.queryResult\\.sortDirection').val('1');
			else if ('sortAsc' == clazz)
				$('#hidden\\.queryResult\\.sortDirection').val('1');
			else
				$('#hidden\\.queryResult\\.sortDirection').val('2');
				
			$('#hidden\\.queryResult\\.pageNumber').val('1');
			$("#hidden\\.doQueryBtn").trigger('click');
			return false;
		});
		
		$("[id^='firstPageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('1');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='prePageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('<c:out value="${prePageNumber}"/>');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='nextPageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('<c:out value="${nextPageNumber}"/>');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='lastPageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('<c:out value="${lastPageNumber}"/>');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='currentPageText']").change(function() {
			var value = this.value;
			if (value != ''){
				value = value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
				var intRegex = /^\d+$/;
				if(!intRegex.test(value)) {
					showErrorMessageDialog('頁數必須是整數');
					return ;
				}
				
				value = parseInt(value, 10);
				
				if (value < 1){
					showErrorMessageDialog('頁數必須是大於1');
					return ;
				}
				else if (value > <c:out value='${lastPageNumber}'/>){
					showErrorMessageDialog('頁數必須是小於總頁數 (<c:out value="${lastPageNumber}"/>)');
					return ;
				}
				else if (value != <c:out value='${currentPageNumber}'/>){
					$('#hidden\\.queryResult\\.pageNumber').val(value);
					$("#hidden\\.doQueryBtn").trigger('click');
					return false;
				}
				
			}
			else{
				showErrorMessageDialog('頁數不可為空白)');
				return ;
			}
		});
		
</c:if>
		
		<c:if test="${not empty actionBean.message}">
    		showMessageDialog('<c:out value="${actionBean.message }" escapeXml="false"/>');
    	</c:if>          
    });
    
    function doPrintEvent()
    {
    	divPrint('listForm');
    	return false;
    }
    
    //-->
    </script>
</head>
<body>
	
	<div id="smallQueryForm"></div>

<stripes:form id="queryUserFrom" beanclass="com.feib.stms.action.Stms9006ActionBean" method="post" onsubmit="javascript: waitingDialog({});">

	<div id="queryForm">
		<table>
			<tr>
				<td colspan="5" class="queryHeader">請輸入查詢條件</td>
			</tr>
			<tr>
				<th>使用者ID</th>
				<td colspan="4">
					<stripes:text id="qryUserIdForm" name="qryUserIdForm" size="12" maxlength="12"/>
				</td>
			</tr>
			<tr>
				<th>姓名</th>
				<td colspan="4">
					<stripes:text id="qryUserNameForm" name="qryUserNameForm" size="20" maxlength="20" />
				</td>
			</tr>
			<tr>
				<td colspan="5" class="queryFunction">
					<stripes:submit id="doQueryBtn" name="doQuery" value="查詢" class="formBtn"/>
					<c:if test="${actionBean.created }">
                    	<stripes:submit id="doCreateBtn" name="doCreate" value="新增" class="formBtn"/>
                    </c:if>
				</td>
			</tr>
		</table>
	</div>
</stripes:form>	
	<!-- 查詢結果  -->
	<div id="listForm">
		<c:if test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		<div id="paginationBar" div stryle=" margin: 0 auto;">
			<input type="button" id="firstPageBtn" class="formBtn" value="第一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			<input type="button" id="prePageBtn" class="formBtn" value="上一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			第 <input type="text" id="currentPageText" class="formBtn" value="${currentPageNumber }" size="3" maxlength="4"> / ${lastPageNumber} 頁
			<input type="button" id="nextPageBtn" class="formBtn" value="下一頁" <c:out value='${lastPageNumber<nextPageNumber ? "disabled": "" }'/> />
			<input type="button" id="lastPageBtn" class="formBtn" value="最末頁" <c:out value='${lastPageNumber<=currentPageNumber ? "disabled": "" }'/> />
		</div>
           <table id="queryResultTable">
			 <thead>
			 	<tr>
                       <th><a id='a-sortHeader-userId' href='javascript:void(0);' class="<c:out value='${userIdSortedClass }'/>"> <c:out value='${userIdSortedSpan }' escapeXml="false"/>使用者ID</a></th>
                       <th><a id='a-sortHeader-userName' href='javascript:void(0);' class="<c:out value='${userNameSortedClass }'/>"> <c:out value='${userNameSortedSpan }' escapeXml="false"/>姓名</a> </th>
                       <th><a id='a-sortHeader-userPhoneNo' href='javascript:void(0);' class="<c:out value='${userPhoneNoSortedClass }'/>"> <c:out value='${userPhoneNoSortedSpan }' escapeXml="false"/>電話</a></th>
                       <th><a id='a-sortHeader-userEmail' href='javascript:void(0);' class="<c:out value='${userEmailSortedClass }'/>"> <c:out value='${userEmailSortedSpan }' escapeXml="false"/>E-MAIL</a></th>
                       <th><a id='a-sortHeader-userLoginControl' href='javascript:void(0);' class="<c:out value='${userLoginControlSortedClass }'/>"> <c:out value='${userLoginControlSortedSpan }' escapeXml="false"/>登入狀態</a></th>                  
                       <th>修改</th>
                </tr>
             </thead>
                   <tbody>
	                   <c:forEach items="${actionBean.queryResult.paginatedList}" var="viewObj" varStatus="rowstat">
	                   <tr>	                  
	                       <td align="center"><c:out value="${viewObj.userId }" /></td>
	                       <td align="center"><c:out value="${viewObj.userName }" /></td>
	                       <td><c:out value="${viewObj.userPhoneNo }" /></td>
	                       <td><c:out value="${viewObj.userEmail }" /></td>
	                       <td>
	                       	<c:if test="${viewObj.userLoginControl.actived }">
	                       		<c:out value="登入" />
	                       	</c:if>
	                       	<c:if test="${!viewObj.userLoginControl.actived }">
	                       		<c:out value="離線" />
	                       	</c:if>  
	                       </td>	                     
	                       <td>
	                       <div id="viewDetail-<c:out value='${viewObj.userId}'/>-viewGroup-<c:out value='${viewObj.group.groupPk}'/>" class="widgetsIcon ui-state-default ui-corner-all" title="檢視"><span class="ui-icon ui-icon-circle-triangle-e"></span></div>
	                       </td>
	                   </tr>
	                   </c:forEach>
                   </tbody>
                   <tfoot>
                   <tr>
                       <td colspan="9" class="listFunction"><input type="button" value="列印" onclick="javascript:doPrintEvent();" class="formBtn"/></td>
                   </tr>
                   </tfoot>
           </table>
		</c:if>             
		</div>

<div style="display: none;">
<stripes:form id="hidden.queryUserFrom" beanclass="com.feib.stms.action.Stms9006ActionBean" method="post" onsubmit="javascript: waitingDialog({});">
	<stripes:hidden id="hidden.qryUserIdForm" name="qryUserIdForm" />
	<stripes:hidden id="hidden.qryUserNameForm" name="qryUserNameForm" />
	<stripes:hidden id="hidden.queryResult.pageNumber" name="queryResult.pageNumber" />
	<stripes:hidden id="hidden.queryResult.sortCriterion" name="queryResult.sortCriterion" />
	<stripes:hidden id="hidden.queryResult.sortDirection" name="queryResult.sortDirection" />
	<stripes:hidden id="hidden.qryUserId" name="qryUserId" />
	<stripes:hidden id="hidden.qryGroupPk" name="qryGroupPk" />
	<stripes:submit id="hidden.doQueryBtn" name="doQuery" value="查詢" class="formBtn"/>
	<stripes:submit id="hidden.doUpdateBtn" name="doUpdate" value="修改" class="formBtn"/>
	<stripes:errors></stripes:errors>
</stripes:form>
</div>

</body>
</html>
